package com.nuc.bigdata.api.controller;

import com.nuc.bigdata.annotation.Permission;
import com.nuc.bigdata.app.service.HomePageService;
import com.nuc.bigdata.app.service.AdminLoginService;
import com.nuc.bigdata.domain.entity.User;
import com.nuc.bigdata.enums.UserRoleEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author JackLuo
 * @date 2019/10/10
 */
@Controller
@RequestMapping("/v1")
public class WelcomeController {

    private final AdminLoginService adminLoginService;

    private final HomePageService homePageService;

    @Autowired
    public WelcomeController(AdminLoginService adminLoginService, HomePageService homePageService) {
        this.adminLoginService = adminLoginService;
        this.homePageService = homePageService;
    }


    /**
     * 欢迎页面
     *
     * @param map 页面数据填充
     * @return index.html
     */
    @GetMapping("/welcome")
    public String welcomePagePad(ModelMap map) {
        //欢迎页填充
        homePageService.init(map);
        return "index";
    }

    /**
     * 打开登录页
     *
     * @param httpSession 存放用户信息
     * @return 如果没有session就加载登录页，有就转发到首页
     */
    @GetMapping("/login")
    public String loadLoginPage(HttpSession httpSession) {
        User user = (User) httpSession.getAttribute(UserRoleEnum.USER.getRoleMsg());
        if (StringUtils.isEmpty(user)) {
            return "sign-in";
        } else {
            return "redirect:" + user.getLoginName() + "/dashboard";
        }
    }

    /**
     * 打开首页
     *
     * @param httpSession 装有用户数据的session
     * @return 首页数据
     */
    @GetMapping("/{loginName}/dashboard")
    @Permission(value = true)
    public String loadHomePage(HttpSession httpSession, ModelMap map) {
        User admin = (User) httpSession.getAttribute(UserRoleEnum.USER.getRoleMsg());
        if (!StringUtils.isEmpty(admin)) {
            return adminPage(admin, map);
        } else {
            return "forward:403";
        }

    }

    /**
     * 是否能够登录
     *
     * @param user        传入的表单对象
     * @param httpSession 用来装表单对象的session
     * @return 首页
     */
    @PostMapping("/login")
    public String toLogin(User user, HttpSession httpSession) throws Exception{
        //是管理员用户，就返回管理员对象
        User admin = adminLoginService.login(user.getLoginName(), user.getPassword(), httpSession);
        if (StringUtils.isEmpty(admin)) {
            return "redirect:login";
        } else {
            return "redirect:" + admin.getLoginName() + "/dashboard";

        }
    }

    private String adminPage(User user, ModelMap map) {
        map.addAttribute("loginName", user.getLoginName());
        map.addAttribute("role", user.getRole());
        return "dashboard";
    }


    @GetMapping("/me")
    public String aboutMe() {
        return "aboutMe";
    }

}
